<?php
function check_login($password = null) {
	global $admin_pass;
	if (isset($password)) {
		if (md5($password) == $admin_pass) {
			$_SESSION['admin'] = 'admin';
			return true;
		}
		return false;
	}
	return isset($_SESSION['admin']);
}
function show_progress_recur($dir_name, $level = 0, $parent = '', $default_info = array()) {
	global $root_dir_name, $file_name_fields, $field_set, $is_admin;
	$dir_base_name = basename($dir_name);
	$general_info = array();
	read_info($root_dir_name . DIRECTORY_SEPARATOR . $dir_name . DIRECTORY_SEPARATOR . $dir_base_name . '.info', $general_info);
	$default_info = array_replace_recursive($default_info, $general_info);
	$dir_full_name = $root_dir_name . DIRECTORY_SEPARATOR . $dir_name;
	if (is_dir($dir_full_name)) {
		$dir = opendir($dir_full_name);
		$entries = browse_dir($dir_full_name, $default_info);
		ksort($entries);
		$level_display = '';
		for ($i = 0; $i < $level; $i++) $level_display .= '&nbsp;&nbsp;->&nbsp;&nbsp;';
		foreach ($entries as $index => $entry) {
			$page = '';
			$order = '';
			$entry_size = sizeof($entry);
			$first_item = true;
			foreach ($entry as $version => $item) {				
				if ($parent != '') $item['order'] = $parent . '.' . $item['order'];
				echo '<tr>';
				if ($first_item) {
					$page = $item['title'];
					$order = $item['order'];
					echo '<td rowspan="' . $entry_size . '">' . $level_display . $item['order'] . '</td>';
					echo '<td rowspan="' . $entry_size . '">' . $item['title'] . '</td>';
				}				
			?>
				<td><?php
				foreach ($item['design'] as $variation => $item_design) {
					echo '<a target="_blank" href="' . $dir_name . '/' . $item_design . '">v' . $item['version'] . '-' . $variation . '</a>';
					if ($item_design != end($item['design'])) echo ', ';
				}
				?></td>
				<?php
				for ($i = 3; $i < sizeof($field_set); $i++) {
					echo '<td>';
					if (isset($item[$field_set[$i]['id']])) {
						$field_i = $item[$field_set[$i]['id']];
						if (isset($field_i['text'])) {
							if (isset($field_i['highlight'])) $field_i['text'] = '<strong>' . $field_i['text'] . '</strong>';
							if (isset($field_i['color'])) $field_i['text'] = '<font color="' . $field_i['color'] . '">' . $field_i['text'] . '</font>';
							if (isset($field_i['url'])) {
								echo '<a target="_blank" href="' . $field_i['url'] . '">' . $field_i['text'] . '</a>';
							} else {
								echo $field_i['text'];
							}
						}
					}					
					echo '</td>';		
				}
				if ($is_admin) {
					echo '<td>';
					show_edit_link($item['file'], 'entry');
					echo ' ';
					$children_dir_full_name = $dir_name . DIRECTORY_SEPARATOR . $page;
					if (is_dir($children_dir_full_name)) {
						$children_info_file_full_name = $children_dir_full_name . DIRECTORY_SEPARATOR . $page . '.info';
						show_edit_link($children_info_file_full_name, 'children');
					}					
					echo '</td>';
				}
				echo '</tr>';
				$first_item = false;
			}
			show_progress_recur($dir_name . DIRECTORY_SEPARATOR . $page, $level + 1, $order, $default_info);
		}
	}
}
function browse_dir($dir_full_name, $default_info) {
	global $file_name_fields, $file_name_pattern, $field_set;
	$entries = array();
	if (is_dir($dir_full_name)) {
		$dir = opendir($dir_full_name);
		while ($entry = readdir($dir)) {
			if (preg_match($file_name_pattern, $entry)) {
				try {
					$file_name = $entry;
					$file_base_name = pathinfo($entry, PATHINFO_FILENAME);
					$parts = explode('-', $file_base_name);
					$info = array();
					$entry_ids = array();
					foreach ($parts as $index => $part) {
						if ($index != (sizeof($field_set) - 1)) $entry_ids[] = $part; // the last part is optional
						$info[$file_name_fields[$index]] = $part;
					}
					$entry_id = implode('-', $entry_ids);
					if (isset($info['variation']) && ((substr($info['variation'], 0, 1) == 'u') || ($info['variation'] > 1))) {
						$entries[$info['order']][$info['version']]['design'][$info['variation']] = $file_name;
					} else {
						$info_file = $dir_full_name . DIRECTORY_SEPARATOR . $entry_id . '.info';
						$info['file'] = $info_file;
						read_info($info_file, $info);
						$info['design'] = array(1 => $file_name);
						$info = array_replace_recursive($default_info, $info);
						$entries[$info['order']][$info['version']] = $info;
					}
				} catch (Exception $e) {}
			}
		}
	}
	return $entries;
}
function get_entry_id($entry) {
	global $field_set, $file_name_fields;
	$file_base_name = pathinfo($entry, PATHINFO_FILENAME);
	$parts = explode('-', $file_base_name);
	$info = array();
	$entry_ids = array();
	foreach ($parts as $index => $part) {
		if ($index != (sizeof($field_set) - 1)) $entry_ids[] = $part; // the last part is optional
	}
	$entry_id = implode('-', $entry_ids);
	return $entry_id;
}
function read_info($file_full_name, &$info) {
	global $field_set_2;
	if (!is_file($file_full_name)) return;
	$lines = file($file_full_name);
	foreach ($lines as $line) {
		$parts = explode("\t", $line);
		$key_set = explode('-', $parts[0]);
		$value = $parts[1];
		$key_set_size = sizeof($key_set);
		if ($key_set_size == 2) {
			$info[$key_set[0]][$key_set[1]] = $value;
		} elseif ($key_set_size == 1) {
			foreach ($field_set_2 as $field) {
				if (!isset($info[$field['id']][$key_set[0]])) {
					$info[$field['id']][$key_set[0]] = $value;
				}
			}
		}
		
	}
}
function show_edit_link ($info_file_full_name, $text) {
	$contents = 'null';
	if (is_file($info_file_full_name)) $contents = file_get_contents($info_file_full_name);
	echo '<a target="_blank" title="' . $contents . '" href="edit_entry.php?file=' . urlencode($info_file_full_name) . '">' . $text . '</a>';
}